﻿using System;
using System.Linq;
using Ones.ServiceAdvertisementManagement;

namespace Ones.WebAdmin
{
    public partial class AdsMgmtBlockPage : System.Web.UI.Page
    {
        private ServiceAdvertisementManagementClient c = null;
        private AdvertisementBlock block = null;
        private string itemId = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["cmd"] != null)
                {
                    fillData();

                    if (Session["cmd"].ToString().Equals("delete"))
                    {
                        txtName.Enabled = false;
                        txtDescr.Enabled = false;
                        txtWidth.Enabled = false;
                        txtHeight.Enabled = false;
                    }
                }
            }
        }

        public void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                block = new AdvertisementBlock();

                block.blkName = txtName.Text;
                block.blkDescription = txtDescr.Text;
                block.blkWidth = int.Parse(txtWidth.Text);
                block.blkHeight = int.Parse(txtHeight.Text);

                if (c.addAdvertisementBlock(block))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListBlockPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                block = new AdvertisementBlock();

                block.blkId = Session["id"].ToString();
                block.blkName = txtName.Text;
                block.blkDescription = txtDescr.Text;
                block.blkWidth = int.Parse(txtWidth.Text);
                block.blkHeight = int.Parse(txtHeight.Text);

                if (c.updateAdvertisementBlock(block))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListBlockPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceAdvertisementManagementClient("adsWsHttpBinding");
                itemId = Session["id"].ToString();

                if (c.deleteAdvertisementBlock(itemId))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/AdsListBlockPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("cmd");
            Response.Redirect("~/WebAdmin/AdsListBlockPage.aspx");
        }
  
        private void fillData()
        {
            c = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");

            itemId = Session["id"].ToString();
            block = c.searchAdvertisementBlockById(itemId);

            txtName.Text = block.blkName;
            txtDescr.Text = block.blkDescription;
            txtWidth.Text = block.blkWidth.ToString();
            txtHeight.Text = block.blkHeight.ToString();

            c.Close();
        }
    }
}